#!/bin/bash
cat > /etc/init.d/overlay << EOF
#!/sbin/runscript

depend() {
	need checkroot
	before hostname
}

start() {
	ebegin "Overlaying files"
	/bin/tar -zx --no-same-owner -C / -f /mnt/cdrom/overlay.tgz
	eend \$? "Failed to overlay files"
	if [ -e /etc/overlay.conf ]; then
		source /etc/overlay.conf
		ebegin "Adding services"
		DONTDEPSCAN=1
		if [ "\${USE_SSH}" == "yes" ]; then
			ln -snf "/etc/init.d/sshd" "/etc/runlevels/default/sshd"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_FW}" == "yes" ]; then
			ln -snf "/etc/init.d/shorewall" "/etc/runlevels/default/shorewall"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_CBQ}" == "yes" ]; then
			ln -snf "/etc/init.d/cbqinit" "/etc/runlevels/default/cbqinit"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_VPN}" == "yes" ]; then
			ln -snf "/etc/init.d/openvpn" "/etc/runlevels/default/openvpn"
			DONTDEPSCAN=0
		fi
		if [ "\${USE_DNSDHCP}" == "yes" ]; then
			ln -snf "/etc/init.d/dnsmasq" "/etc/runlevels/default/dnsmasq"
			DONTDEPSCAN=0
		fi
		if [ -n "\${NTPSERVER}" ]; then
			ln -snf "/etc/init.d/ntpd" "/etc/runlevels/default/ntpd"
			DONTDEPSCAN=0
		fi
		eend \${DONTDEPSCAN} "No service to add"

		if [ -n "\${NBCARDS}" ]; then
                  if [ \${NBCARDS} -ge 2 ]; then
		  	ebegin "Adding network cards"
                        ln -snf "/etc/init.d/net.lo" "/etc/init.d/net.eth1"
			ln -snf "/etc/init.d/net.eth1" "/etc/runlevels/default/net.eth1"
			DONTDEPSCAN=0
			if [ \${NBCARDS} -ge 3 ]; then
                                ln -snf "/etc/init.d/net.lo" "/etc/init.d/net.eth2"
				ln -snf "/etc/init.d/net.eth2" "/etc/runlevels/default/net.eth2"
			fi
		  	if [ \${NBCARDS} -ge 4 ]; then
                                ln -snf "/etc/init.d/net.lo" "/etc/init.d/net.eth3"
				ln -snf "/etc/init.d/net.eth3" "/etc/runlevels/default/net.eth3"
		  	fi
			eend 0
		  fi
		fi

		if [ \${DONTDEPSCAN} -eq 0 ]; then
			/sbin/depscan.sh
			eend 0
		fi
		
		if [ -n "\${NTPSERVER}" ]; then
			ebegin "Setting NTP server location"
			echo "server \${NTPSERVER}" >> /etc/ntpd.conf
			#This needs openntpd >=3.6p1
			#sed -i "s/^NTPD_OPTS=.*$/NTPD_OPTS=\"-s\"/" /etc/conf.d/ntpd
			eend \$? "Failed to set NTP server"
		fi
		if [ "\${NET_CONFIG}" == "dhcp" ]; then
			ebegin "Enabling DHCP"
			sed -i "s/^DHCP=\"yes\"/DHCP=\"no\"/" /etc/init.d/autoconfig
			eend \$? "Failed to enable DHCP"
		fi
		if [ -n "\${TIMEZONE}" ]; then
			ebegin "Setting timezone to \$TIMEZONE"
			echo \${TIMEZONE} > /etc/TZ
			eend \$? "Failed to set timezone"
		fi
		if [ -n "\${KEYMAP}" ]; then
			ebegin "Setting keyboard map to \$KEYMAP"
			sed -i "s/^KEYMAP=\"us\"/KEYMAP=\"\$KEYMAP\"/" /etc/rc.conf
			eend \$? "Failed to set keymap"
		fi
		if [ "\${IP_RELAY}" == "yes" ]; then
			ebegin "Allowing IP routing"
			sed -i "s/#net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/" /etc/sysctl.conf
			eend \$? "Failed to modify sysctl.conf"
		fi
	fi
	return 0
}
EOF
chmod a+x /etc/init.d/overlay

# Copy net.ethX scripts
ln -snf "/etc/init.d/net.lo" "/etc/init.d/net.eth0"

# We are a liveCD
echo "export CDBOOT=yes" >> /sbin/functions.sh

# We don't want fstab to try to swapon /dev/SWAP by default
sed -i "s/\/dev\/SWAP/#\/dev\/SWAP/" /etc/fstab

# Undo livecdfs-update damage
rc-update add keymaps boot
echo "This is \n.\O (\s \m \r) \t" > /etc/issue

# Add overlay to boot critical services
rc-update add overlay boot

cat > /etc/runlevels/boot/.critical << EOF
checkroot overlay hostname modules checkfs localmount
EOF

cat >> /etc/inittab << EOF
# Serial console login
s0:12345:respawn:/sbin/agetty -L -i 19200 ttyS0 vt100
EOF
